//
//  ViewController.m
//  apk的下载模拟
//
//  Created by Jorn.Wu on 15/7/16.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"
#import "CellModelViewController.h"


#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor grayColor];
    [self addCellModels];
    [self AddNavigation];
    self.title = @"装机必备";
}
- (void)AddNavigation{
    UINavigationBar *navigation = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:navigation];
    
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];
    UIBarButtonItem *finishBtn = [[UIBarButtonItem alloc] initWithTitle:@"已下载" style:UIBarButtonItemStylePlain target:self action:nil];
    
    UINavigationItem *nvgItem = [[UINavigationItem alloc] initWithTitle:@"装机必备"];
    nvgItem.leftBarButtonItem = backBtn;
    nvgItem.rightBarButtonItem = finishBtn;
    
    navigation.items = @[nvgItem];
    
    
}
- (void)addCellModels{
    
    NSMutableArray *imgAr = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
    NSArray *listAr = [NSArray arrayWithContentsOfFile:path];
    
    
    int num = 12;
    int col = 4;
    CGFloat cellW = 70;
    CGFloat cellH = 110;
    
    CGFloat pading = (kScreenWidth - cellW * col) / (col + 1);
    
    for (int i = 0; i < num; i++) {
        UIImage *img = [UIImage imageNamed:[listAr[i] objectForKey:@"icon"]];
        [imgAr addObject:img];
    }
    
    for (int i = 0; i < num; i++) {
        
        int C = i % col;
        int R = i / col;
        
        
        CellModelViewController *cell = [[CellModelViewController alloc] init];
        cell.view.frame = CGRectMake((pading + cellW) * C + pading, 64 + (pading + cellH) * R + pading, cellW, cellH);
        
        cell.imageView.image = imgAr[i];
        cell.titleLabel.text = [listAr[i] objectForKey:@"name"];
        cell.dowLoadBtn.tag = i + 100;
        
        UIButton *btn = cell.dowLoadBtn;
        
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:cell.view];
        
    }
    
    
}
- (void)btnAction:(UIButton *)btn{
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn setTitle:@"正在下载..." forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitle:@"已下载" forState:UIControlStateNormal];//应经在xib设置了
        btn.enabled = NO;
    });
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
